package cn.mafangui.hotel.controller.worker;

import cn.mafangui.hotel.entity.User;
import cn.mafangui.hotel.entity.UserVipLevel;
import cn.mafangui.hotel.mapper.UserVipLevelMapper;
import cn.mafangui.hotel.response.AjaxResult;
import cn.mafangui.hotel.response.ResponseTool;
import cn.mafangui.hotel.service.UserService;
import cn.mafangui.hotel.utils.MD5Utils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping(value = "/op/vipLevel")
@CrossOrigin("*")
public class OpVipLevelController {

    @Resource
    private UserVipLevelMapper userVipLevelMapper;

    @Resource
    private UserService userService;

    /**
     * 所有等级
     * @return
     */
    @RequestMapping(value = "")
    public AjaxResult getAllRoomType(){
        List<UserVipLevel> all = userVipLevelMapper.findAll();
        return ResponseTool.success(all);
    }

    /**
     * 同意充值
     * @return
     */
    @PostMapping(value = "/pay")
    public AjaxResult sendPay(Integer userId){
        User user = userService.selectById(userId);
        if (user == null){
            return ResponseTool.failed("用户不存在");
        }
        int level = 1;
        for (UserVipLevel vipLevel : userVipLevelMapper.findAll()) {
            if (user.getPaymoney() >= vipLevel.getTomoney()){
                level = Integer.parseInt(vipLevel.getViplevel());
            }
        }
        user.setViplevel(level);
        user.setMoney(user.getMoney()+user.getPaymoney());
        user.setPaymoney(0.0);
        if(userService.updateUser(user)==1)
            return ResponseTool.success("充值成功");
        return ResponseTool.failed("充值失败");
    }

    /**
     * add
     * @return
     */
    @PostMapping(value = "/add")
    public AjaxResult add(String viplevel,Double discount,Integer tomoney,String intro){
        UserVipLevel userVipLevel = new UserVipLevel(0,viplevel,discount,tomoney,intro);
        userVipLevelMapper.insert(userVipLevel);
        return ResponseTool.success();
    }

    /**
     * finfbyid
     * @return
     */
    @GetMapping(value = "{id}")
    public AjaxResult getbyid(@PathVariable("id") Integer id){
        return ResponseTool.success(userVipLevelMapper.selectByPrimaryKey(id));
    }

    /**
     * finfbyid
     * @return
     */
    @DeleteMapping(value = "{id}")
    public AjaxResult del(@PathVariable("id") Integer id){
        return ResponseTool.success(userVipLevelMapper.deleteByPrimaryKey(id));
    }

    /**
     * update
     * @return
     */
    @PostMapping(value = "/update")
    public AjaxResult update(Integer id,String viplevel,Double discount,Integer tomoney,String intro){
        UserVipLevel userVipLevel = new UserVipLevel(id,viplevel,discount,tomoney,intro);
        userVipLevelMapper.updateByPrimaryKeySelective(userVipLevel);
        return ResponseTool.success();
    }
}
